﻿using System;
using OnlineGameLibrary.Game;
using System.Linq;
using System.ComponentModel.Composition;
using OnlineGameLibrary.Contract;

namespace OnlineGameClient.ViewModel
{
    public class GameImporter : IPartImportsSatisfiedNotification
    {
        public event EventHandler ImportChanged;
        public GameImporter()
        {
            CompositionInitializer.SatisfyImports(this);
        }

        [ImportMany(AllowRecomposition = true)]
        public Lazy<IGameLoader, IGameLoaderAttributes>[] Games { set; get; }

        public GameBase CreateGame(string gameIdentity,GameRoom gameroom,Player curplayer,IPlayerDuplexChannel channel)
        {
            var gamepair = Games.FirstOrDefault(lz => lz.Metadata.IdentityName == gameIdentity);
            if (gamepair != null)
                return gamepair.Value.CreateGame(gameroom, curplayer, channel);
            else
                return null;
        }

        public void OnImportsSatisfied()
        {
            if (ImportChanged != null)
                ImportChanged(this, new EventArgs());
        }
    }
}
